import 'package:flutter/material.dart';

class PullRefreshTest extends StatefulWidget {
  @override
  _PullRefreshTestState createState() => _PullRefreshTestState();
}

class _PullRefreshTestState extends State<PullRefreshTest> {
  final int pageSize = 30;
  bool disposed = false;
  List<String> dataList = new List();
  final ScrollController _controller = new ScrollController();
  final GlobalKey<RefreshIndicatorState> refreshKey = new GlobalKey();

  Future<void> onRefresh() async {
    await Future.delayed(Duration(seconds: 1));
    dataList.clear();
    for (int i = 0; i < pageSize; i++) {
      dataList.add("refresh");
    }
    if (disposed) {
      return;
    }
    setState(() {});
  }

  Future<void> loadMore() async {
    await Future.delayed(Duration(seconds: 1));
    for (int i = 0; i < pageSize; i++) {
      dataList.add("loadmore");
    }
    if (disposed) {
      return;
    }
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    _controller.addListener(() {
      if (_controller.position.pixels == _controller.position.maxScrollExtent) {
        loadMore();
      }
    });
    Future.delayed(Duration(seconds: 0), () {
      refreshKey.currentState.show();
    });
  }

  @override
  void dispose() {
    disposed = true;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('RefreshTest'),
      ),
      body: Container(
        child: RefreshIndicator(
          key: refreshKey,
          onRefresh: onRefresh,
          child: ListView.builder(
            physics: const AlwaysScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              if (index == dataList.length) {
                return Container(
                  margin: EdgeInsets.all(10),
                  child: Align(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        SizedBox(
                          width: 15,
                          height: 15,
                          child: CircularProgressIndicator(
                            strokeWidth: 2,
                          ),
                        ),
                        Padding(
                          padding: EdgeInsets.only(left: 20),
                          child: Text('正在加载...'),
                        )

                      ],
                    ),
                  ),
                );
              }
              return Card(
                child: InkWell(
                  child: GestureDetector(
                    child: Container(
                      height: 60,
                      padding: EdgeInsets.all(10),
                      alignment: Alignment.centerLeft,
                      child: Text('item ${dataList[index]} $index'),
                    ),
                    onTap: (){
                      print(dataList[index]);
                      Scaffold.of(context).showSnackBar(SnackBar(
                        content: Text(dataList[index]),
                      ));
                    },
                  ),
                ),
              );
            },

            itemCount: (dataList.length >= pageSize)
                ? dataList.length + 1
                : dataList.length,
            controller: _controller,
          ),
        ),
      ),
    );
  }
}
